-- Keymaps are automatically loaded on the VeryLazy event
-- Default keymaps that are always set: https://github.com/LazyVim/LazyVim/blob/main/lua/lazyvim/config/keymaps.lua
-- Add any additional keymaps here

-- 添加逗号后保留光标位置
vim.keymap.set("i", "<A-,>", "<C-o>a,<CR>", { desc = "Insert comma and stay in insert mode" })
-- 添加冒号后保留光标
vim.keymap.set("i", "<A-;>", "<C-o>a:<CR>", { desc = "Insert colon and stay in insert mode" })

-- vim.keymap.set("n", "<leader>tv", "<cmd>vsplit<cr><cmd>terminal<cr>i", { desc = "Terminal vertical split" })
local term_toggle = function()
  local term_buf_name = "term://toggle-terminal"
  local bufnr = vim.fn.bufnr(term_buf_name)

  if bufnr == -1 then
    -- Create new terminal
    vim.cmd("vsplit | terminal")
    vim.cmd("file " .. term_buf_name)
  else
    -- Check if terminal is visible in any window
    local wins = vim.fn.getbufinfo(bufnr)[1].windows
    if #wins > 0 then
      -- Terminal is visible, hide it
      vim.api.nvim_win_hide(wins[1])
    else
      -- Terminal exists but hidden, show it in split
      vim.cmd("vsplit | buffer " .. bufnr)
    end
  end
end

vim.keymap.set("n", "<C-;>", term_toggle, { desc = "Toggle terminal" })
vim.keymap.set("t", "<C-;>", "<C-\\><C-n><Cmd>lua term_toggle()<CR>", { desc = "Toggle terminal", noremap = true })
